﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Thero : MonoBehaviour{
	
	/// <summary>
	/// 英雄的类型
	/// </summary>
	public enum HeroTypeEnum
	{
		Leader,										//领导者
		Team,										//队员
	}
	
	const int OFFSET_POS = 4;						//设置偏移位数
	const int OFFSET_CONSTANT = 15;					//偏移常量
	
	public UITable leaderTable;						//领导者表
	public UITable teamTable;						//队员表
	public UITable heroTable;						//所有的hero Table
	
	TSprite leader;									//队伍领导者
	List<TSprite> teamList = new List<TSprite>();	//队伍队员列表
	
	static Thero instance;
	
	public static Thero GetInstance()
	{
		return instance;
	}
	
	void Awake()
	{
		instance = this;
	}
	
	void Start()
	{
		Init();
	}
	
	void OnGUI()
	{
		if(GUILayout.Button("   Bulet1  "))
		{
			Leader.CurrBulletLevel = BulletLevelEnum.One;
		}
		if(GUILayout.Button("   Bulet2  "))
		{
			Leader.CurrBulletLevel = BulletLevelEnum.Two;
		}
		if(GUILayout.Button("   Bulet3  "))
		{
			Leader.CurrBulletLevel = BulletLevelEnum.Three;
		}
		if(GUILayout.Button("   Bulet4  "))
		{
			Leader.CurrBulletLevel = BulletLevelEnum.Four;
		}
		if(GUILayout.Button("   Bulet5  "))
		{
			Leader.CurrBulletLevel = BulletLevelEnum.Five;
		}
		
	}
	
	public void Init ()
	{
		HeroSacle();
		ResetCollider();
	}
	
	public void OnUpdate ()
	{
		
	}
	
	/// <summary>
	/// R场景资源加载完毕之后的数据操作
	/// </summary>
	public void OnLoadOverMap()
	{
//		Invoke("RepositionTable", 0.1f);	//延迟一会刷新对象， 刚加载完的那一帧,还没有赋值需要在下一帧才能获得数据
		RepositionTable();
	}
	
	/// <summary>
	/// 英雄图片进行缩放控制
	/// </summary>
	void HeroSacle()
	{
		if(ViewRect.GetInstance())
		{
			transform.localScale = ViewRect.GetInstance().HeroScaleV;
		}
	}
	
	/// <summary>
	/// 重新设置碰撞盒
	/// </summary>
	void ResetCollider()
	{
		if(transform.collider != null)
		{
			Destroy(transform.collider);
		}
		BoxCollider box = NGUITools.AddWidgetCollider(gameObject);
		box.size = new Vector3(Screen.width, Screen.height, 1) * 4;
	}
	
	/// <summary>
	/// 根据下载的index 顺序设置偏移数据
	/// </summary>
	public int GetOffsetHeroType(int index, string url)
	{
		return index << OFFSET_POS | (int)HeroTypeEnum.Leader;		//暂时先都定为领导者
	}
	
	/// <summary>
	/// 根据 偏移之后的数据获得 英雄的 下载 Index
	/// </summary>
	public int GetHeroIndex(int parameter)
	{
		return parameter >> OFFSET_POS;
	}
	
	/// <summary>
	/// 根据偏移之后的数据 获得英雄的类型
	/// </summary>
	public HeroTypeEnum GetHeroType(int parameter)
	{
		return (HeroTypeEnum)(parameter & OFFSET_CONSTANT);
	}
	
	/// <summary>
	/// 设置英雄的类型
	/// </summary>
	public void SetHeroType(GameObject go, HeroTypeEnum type)
	{
		switch(type)
		{
		case HeroTypeEnum.Leader:
			go.transform.parent = leaderTable.transform;
			Leader = go.GetComponent<TTeam>();
			break;
		case HeroTypeEnum.Team:
			go.transform.parent = teamTable.transform;
			TeamList.Add(go.GetComponent<TTeam>());
			break;
		}
		
	}
	
	public List<TSprite> GetHeroList()
	{
		List<TSprite> list = new List<TSprite>();
		list.Add(Leader);
		list.AddRange(TeamList);
		
		return list;
	}
	
	/// <summary>
	/// 刷新Table 表数据
	/// </summary>
	void RepositionTable()
	{
		leaderTable.Reposition(true);
		teamTable.Reposition(true);
		heroTable.Reposition(true);
	}
	
	public TSprite Leader
	{
		get{return leader;}
		set{leader = value;}
	}
	
	public List<TSprite> TeamList
	{
		get{return teamList;}
		set{teamList = value;}
	}
}
